package com.tentcoo.zhaogonggu.module.homepage.b;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.os.Looper;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

import com.tentcoo.zhaogonggu.R;

/**
 * Created by Gunter on 2015/4/22.
 */
public class DiamondAttainTab extends View {
    private String mText;
    private Bitmap mIcon;
    private float mTextSize;
    private int mColor;
    private int alpha;
    private int textX,textY;
    private Rect iconRect, textRect;

    public DiamondAttainTab(Context context) {
        this(context, null);
    }

    public DiamondAttainTab(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public DiamondAttainTab(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.DiamondAttainTab);
        int attrCount = typedArray.getIndexCount();
        for (int i = 0; i < attrCount; i++) {
            int index = typedArray.getIndex(i);
            switch (index) {
                case R.styleable.DiamondAttainTab_mColor:
                    mColor = typedArray.getColor(index, 0xFF6600);
                    break;
                case R.styleable.DiamondAttainTab_mText:
                    mText = typedArray.getString(index);
                    break;
                case R.styleable.DiamondAttainTab_mTextSize:
                    mTextSize = typedArray.getDimension(index, TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 20, getResources().getDisplayMetrics()));
                    break;
                case R.styleable.DiamondAttainTab_tabIcon:
                    BitmapDrawable bitmapDrawable = (BitmapDrawable) typedArray.getDrawable(index);
                    mIcon = bitmapDrawable.getBitmap();
                    break;
            }
        }
        typedArray.recycle();
        alpha = 0;
        iconRect = new Rect();
        textRect = new Rect();
        Paint textPaint = new Paint();
        textPaint.setTextSize(mTextSize);
        textPaint.getTextBounds(mText, 0, mText.length(), textRect);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        iconRect.set(0, 0, getMeasuredWidth(), getMeasuredHeight());
        textX = (getMeasuredWidth() - textRect.width()) / 2;
        textY = iconRect.top + (getMeasuredHeight() - textRect.height()) / 2;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawBitmap(mIcon, null, iconRect, null);
        canvas.drawBitmap(getIconColorBitmap(alpha, mColor, textX, textY), 0, 0, null);
        drawSourceTextBitmap(canvas, textX, textY, 0xFFF);
        drawTargetTextBitmap(canvas, textX, textY, 255 - alpha);
    }

    private Bitmap getIconColorBitmap(int alpha, int color, int textX, int textY) {
        Bitmap iconColorBitmap = Bitmap.createBitmap(getMeasuredWidth(), getMeasuredHeight(), Bitmap.Config.ARGB_8888);
        Canvas tempCanvas = new Canvas(iconColorBitmap);
        Paint tempPaint = new Paint();
        tempPaint.setAntiAlias(true);
        tempPaint.setColor(color);
        tempPaint.setAlpha(alpha);
        tempCanvas.drawRect(iconRect, tempPaint);
        drawSourceTextBitmap(tempCanvas, textX, textY, 0xFFF);
        tempPaint.setAlpha(255);
        tempPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
        tempCanvas.drawBitmap(mIcon, null, iconRect, tempPaint);
        return iconColorBitmap;
    }

    private void drawTargetTextBitmap(Canvas canvas, int textX, int textY, int alpha) {
        Paint textPaint = new Paint();
        textPaint.setColor(mColor);
        textPaint.setTextSize(mTextSize);
        textPaint.setAlpha(alpha);
        canvas.drawText(mText, textX, textY, textPaint);
    }

    private void drawSourceTextBitmap(Canvas canvas, int textX, int textY, int color) {
        Paint textPaint = new Paint();
        textPaint.setColor(color);
        textPaint.setTextSize(mTextSize);
        canvas.drawText(mText, textX, textY, textPaint);
    }

    public void setAlpha(int alpha) {
        this.alpha = alpha;
        invalidateView();
    }

    private void invalidateView() {
        if (Looper.myLooper() == Looper.getMainLooper()) {
            invalidate();
        } else {
            postInvalidate();
        }
    }
}
